const utils = require("../../../../common/util/util")
let text = require('../../../../common/util/text.js')
Page({
    data: {
        using : '' ,
        moreState : false ,
        content : '' ,
        state : '' ,
        statusBar: '',
        customBar: '',
        statusBarH:'',
        id : ''
    },
    
    onLoad: function (opt) {
        this.setData({
          id:opt.id
        })
        this.xInit(opt.id)
        wx.getSystemInfo({
          success: (e) => {
            let custom = wx.getMenuButtonBoundingClientRect();
            this.setData({
              statusBar: e.statusBarHeight,
              customBar: custom.bottom + custom.top - e.statusBarHeight,
              statusBarH: Number(e.statusBarHeight)*2
            })
          }
        })
    },
    trynext: function () {
        if(this.data.id=='deteriorating_risk'){
          wx.reLaunch({
            url: '/subuser/pages/research_deteriorate/index',
          })
        }else if(this.data.id=='comprehensive_analysis'){
          wx.reLaunch({
            url: '/subuser/pages/improveData/index',
          })
        }else{
          wx.reLaunch({
            url: '../index/index',
          })
        }
    },
    toback: function () {
        wx.navigateBack({
          delta:1
        })
    },
    xInit: function (id) {
        let that = this
        if(id=='comprehensive_analysis'){
          // 综合
          wx.cloud.callFunction({
            name: 'user_extends',
            data: {
              action: "get_comprehensive_analysis_history"
            }
          }).then(res => {
              let data = res.result
              for(var i in data){
                  data[i].name='疼痛影响因素综合分析'
                  data[i].create_time = utils.formatDate_chinese(new Date(data[i].create_time))
                  let [anxious_name,depressed_name,pressure_name,sleep_scale_value_name,pesq_self_efficacy_scale_name]=['','','','','']
                  // 焦虑
                  if(data[i].anxious<=7){
                    anxious_name='正常'
                  }else if(data[i].anxious>7 && data[i].anxious<=9){
                    anxious_name='轻度'
                  }else if(data[i].anxious>9 && data[i].anxious<=14){
                    anxious_name='中度'
                  }else if(data[i].anxious>14 && data[i].anxious<=19){
                    anxious_name='重度'
                  }else if(data[i].anxious>=20){
                    anxious_name='非常严重'
                  }
                  // 抑郁
                  if(data[i].depressed<=9){
                    depressed_name='正常'
                  }else if(data[i].depressed>9 && data[i].depressed<=13){
                    depressed_name='轻度'
                  }else if(data[i].depressed>13 && data[i].depressed<=20){
                    depressed_name='中度'
                  }else if(data[i].depressed>20 && data[i].depressed<=27){
                    depressed_name='重度'
                  }else if(data[i].depressed>=28){
                    depressed_name='非常严重'
                  }
                  // 压力
                  if(data[i].pressure<=14){
                    pressure_name='正常'
                  }else if(data[i].pressure>14 && data[i].pressure<=18){
                    pressure_name='轻度'
                  }else if(data[i].pressure>18 && data[i].pressure<=25){
                    pressure_name='中度'
                  }else if(data[i].pressure>25 && data[i].pressure<=33){
                    pressure_name='重度'
                  }else if(data[i].pressure>=34){
                    pressure_name='非常严重'
                  }
                  // 睡眠
                  if(data[i].sleep_scale_value<=4){
                    sleep_scale_value_name='无失眠'
                  }else if(data[i].sleep_scale_value>4 && data[i].sleep_scale_value<=6){
                    sleep_scale_value_name='怀疑失眠'
                  }else if(data[i].sleep_scale_value>6){
                    sleep_scale_value_name='失眠'
                  }
                  // 自我效能
                  if(data[i].pesq_self_efficacy_scale<20){
                    pesq_self_efficacy_scale_name='低'
                  }else if(data[i].pesq_self_efficacy_scale>=20 && data[i].pesq_self_efficacy_scale<=40){
                    pesq_self_efficacy_scale_name='中等'
                  }else if(data[i].pesq_self_efficacy_scale>40){
                    pesq_self_efficacy_scale_name='高'
                  }
                  let str =''
                  // for(let index in data[i].psychosocial_model){
                  //   str+='<p>'+ data[i].psychosocial_model[index].name+ ':'+ data[i].psychosocial_model[index].v +'</p>'
                  // }
                  data[i].content_value ='焦虑:'+ anxious_name+'; 抑郁:'+ depressed_name+'; 压力:'+ depressed_name+'; 睡眠:'+ sleep_scale_value_name+'; 自我效能:'+ pesq_self_efficacy_scale_name
                  data[i].content = '<p>焦虑:' + anxious_name + '</p><p>抑郁:' + depressed_name +'</p><p>压力:' + pressure_name+'</p><p>失眠:'+ sleep_scale_value_name + '</p><p>自我效能:' + pesq_self_efficacy_scale_name +'</p>'+str
              }
              that.setData({
                  using : data
              })
          })

        }else if(id=='deteriorating_risk'){
          // 恶化
          wx.cloud.callFunction({
            name: 'user_extends',
            data: {
              action: "get_deteriorating_risk_history"
            }
          }).then(res => {
            let data = res.result
            for(var i in data){
              data[i].name='你的疼痛恶化风险有多高'
              data[i].create_time = utils.formatDate_chinese(new Date(data[i].create_time))
            }
            that.setData({
                using : data
            })
          })
        }else{
          wx.cloud.callFunction({
            name: 'scale_manage',
            data: {
                action: "get_scale_return_by_user",
                dataObj:{
                    scale_number: id
                }
            }
          }).then(res => {
              let data = res.result
              for(var i in data){
                  data[i].create_time = utils.formatDate_chinese(new Date(data[i].create_time))
              }
              that.setData({
                  using : data
              })
          })
        }
    },

    xMore: function (e) {
        let that = this
        let params = e.currentTarget.dataset
        that.setData({
            state : params.state ,
            content : text.text(params.content) ,
            moreState : true
        })
    },

    reb: function () {
        let that = this
        let using = that.data.using
        wx.requestSubscribeMessage({
            tmplIds: [
                "XtY2dphoow0teHS_Jo0nctaYxdQhs-QqsiokLDlJ8yM"
            ],
            success: (res) => {
                if (res['XtY2dphoow0teHS_Jo0nctaYxdQhs-QqsiokLDlJ8yM'] === 'accept') {
                    wx.cloud.callFunction({
                        name: 'message_subscribe',
                        data: {
                            action: "add_subscribe",
                            dataObj: {
                                templateid: "XtY2dphoow0teHS_Jo0nctaYxdQhs-QqsiokLDlJ8yM",
                                subscribe_status_string: "accept"
                            }
                        }
                    }).then(res => {
                        wx.showToast({
                            title: '订阅成功',
                            icon: 'none',
                            duration: 1000,
                        })
                    })
                }
            },
            fail() { },
            complete() {
                wx.cloud.callFunction({
                    name: 'message_subscribe',
                    data: {
                        action: "add_message_subscribe",
                        dataObj:{
                            templateid:"XtY2dphoow0teHS_Jo0nctaYxdQhs-QqsiokLDlJ8yM",
                            subscribe_status_string:"accept",
                            param: using[0].scale_number?using[0].scale_number:''
                       }
                    }
                }).then(res => {
                    console.log("返回:", res.result)
                })
            }
        })
    }
})